package com.amit.jsf;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

public class SIDConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value)
	throws ConverterException{
		
		System.out.println("SIDConverter-getAs Object");
		
		String sid="";
		
		String msg=component.getId();
		
		FacesMessage message=new FacesMessage();
		
		int id=0;
		
		
		try
		{
			if(value==null || value.length()==0 )
			{
				message.setDetail(msg+" is Required");
				throw new ConverterException(message);
			}
			
			id=Integer.parseInt(value);
		}
		
		catch (NumberFormatException e)
		{
			
			message.setDetail("Please enter Only Digits for"+msg);
			
			throw new ConverterException(message);
			
			
		}
		
		if(id>=1 && id<=999)
		{
			sid="JLC-"+id;
		}
		
		else
		{
			message.setDetail("Please enter value between 1 and 999"+msg);
			
			throw new ConverterException(message);
		}
		
		return sid;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object value) {
		
		String str=value.toString();
		String val=str.substring(4);
		
		return val;
	}
	
	

}
